<?php
/**
* 系统用户模型
* lsj 2010-11-20
*/

if(!defined('APP_HOME')) exit('Access Denied');

class goodsMod extends lib_model
{
    protected $tbl = 'goods';#当前模型操作的主表
    protected $jtblCate = 'goods_cate';#要连接的商品类别表名
    protected $jtblMerchant = 'merchant';#要连接的商家表名
    protected $jtblUser = 'sys_users';#要连接的用户表名
    protected $jtblCity = 'sys_citys';#要连接的用户表名
	protected $key = array(
		'goods_cate' => 'goods.goods_cate_id', 
		'merchant' => 'goods.merchant_id', 
		'sys_user' => 'goods.sys_users_id',
		'sys_citys' => 'goods.sys_citys_id',
	);#当前模型要连接的表与对应外键

	/*
	#初始化
	*/
	public function _init()
	{
        $this->ckName = loadConfig('user');
	}

 
    /*
    #检查商品是否存在
    */
    public function isGoods($d)
    {
        $res = $this->setId($d)->select('id');
        if(null === $res) return 0;#不存在
        elseif(intval($res)) return 1;#存在
        return false;#异常
    }
    
	/*
    #检查商品是否处在出售状态
    */
    public function isShoping($d)
    {
        $res = $this->setId($d)->select('id');
        if(null === $res) return 0;#不存在
        elseif(intval($res)) return 1;#存在
        return false;#异常
    }
    
	/*
    #检查商品是否存在
    */
    public function isGoods($d)
    {
        $res = $this->setId($d)->select('id');
        if(null === $res) return 0;#不存在
        elseif(intval($res)) return 1;#存在
        return false;#异常
    }

    /*
    #获取当前正在销售的商品们
    */
    public function getNowGoods()
    {
       
    }

    /*
    #获取商品信息
    */
    public function getGoods($id = 0, $sel = '*')
    {
       if(0 !== $id){
			if(is_array($id)) $id = join(',', $id);
			else $this->setLimit(1);
			$this->setId($id);
		}
		//$this->setDebug();
		return $this->select($sel);
    }

	/*
	 *
	 */
	public function addGoods()
	{

	}

	/*
	 *
	 */
	public function delGoods()
	{

	}

	/*
	 *
	 */
	 public function editGoods()
	 {

	 }
	/*
	 * 获取商品的状态：'none','success','soldout','failure','refund'
	 */
	public function getState($d)
	{

	}

    
    
	/*
	 * 对各字段的过滤， 用于过滤客户端提交来的数据
	 * 只对字段类型做处理，对长度不做处理，请在控制器做长度的处理
	 */
	public function filterItem($item)
	{
		if(empty($item)) return array();
		
		if(isset($item['id'])) $item['id'] = abs(intval($item['id']));
		if(isset($item['sys_users_id'])) $item['sys_users_id'] = abs(intval($item['sys_users_id']));
		if(isset($item['goods_cate_id'])) $item['goods_cate_id'] = abs(intval($item['goods_cate_id']));
		if(isset($item['sys_citys_id'])) $item['sys_citys_id'] = abs(intval($item['sys_citys_id']));
		if(isset($item['express_id'])) $item['express_id'] = abs(intval($item['express_id']));
		if(isset($item['title'])) $item['title'] = trim($item['title']);
		if(isset($item['stitle'])) $item['stitle'] = trim($item['stitle']);
		if(isset($item['teammode'])) $item['teammode'] = trim($item['teammode']);
		if(isset($item['summary'])) $item['summary'] = trim($item['summary']);
		
		if(isset($item['notice'])) $item['notice'] = trim($item['notice']);
		if(isset($item['images'])) $item['images'] = trim($item['images']);
		if(isset($item['detail'])) $item['detail'] = trim($item['detail']);
		if(isset($item['myreview'])) $item['myreview'] = trim($item['myreview']);
		if(isset($item['hisreview'])) $item['hisreview'] = trim($item['hisreview']);
		
		if(isset($item['teamprice'])) $item['name'] = float(trim($item['teamprice']));
		if(isset($item['marketprice'])) $item['marketprice'] = float(trim($item['marketprice']));
		
		if(isset($item['delivery'])) $item['delivery'] = trim($item['delivery']);
		if(isset($item['iscredit'])) $item['iscredit'] = trim($item['iscredit']);
		if(isset($item['ispoint'])) $item['ispoint'] = trim($item['ispoint']);
		if(isset($item['ishide'])) $item['ishide'] = trim($item['ishide']);
		if(isset($item['state'])) $item['state'] = trim($item['state']);

		if(isset($item['freefromnum'])) $item['freefromnum'] = abs(intval($item['freefromnum']));
		if(isset($item['pernumber'])) $item['pernumber'] = abs(intval($item['pernumber']));
		if(isset($item['minnumber'])) $item['minnumber'] = abs(intval($item['minnumber']));
		if(isset($item['maxnumber'])) $item['maxnumber'] = abs(intval($item['maxnumber']));
		if(isset($item['nowpnumber'])) $item['nowpnumber'] = abs(intval($item['nowpnumber']));
		if(isset($item['nowgnumber'])) $item['nowgnumber'] = abs(intval($item['nowgnumber']));
		if(isset($item['addnumber'])) $item['addnumber'] = abs(intval($item['addnumber']));
		if(isset($item['expiretime'])) $item['expiretime'] = abs(intval($item['expiretime']));
		if(isset($item['begintime'])) $item['begintime'] = abs(intval($item['begintime']));
		if(isset($item['endtime'])) $item['endtime'] = abs(intval($item['endtime']));

		if(isset($item['closetime'])) $item['closetime'] = abs(intval($item['closetime']));
		if(isset($item['createtime'])) $item['createtime'] = abs(intval($item['createtime']));
		if(isset($item['goodsweight'])) $item['goodsweight'] = abs(intval($item['goodsweight']));
		if(isset($item['credit'])) $item['credit'] = abs(intval($item['credit']));
		
		return $item;
	}


    #################################################


	/*
	#设置商品ID
	*/
	public function setId($d, $c = 'is')
	{
		$this->setWhere($this->tbl . '.id', $d, $c);
        return $this;
	}

	/*
	#设置商品标题
	*/
	public function setTitle($d, $c = 'is')
	{
		$this->setWhere($this->tbl . '.title', $d, $c);
        return $this;
	}
	
	/*
	#设置商品副标题
	*/
	public function setStitle($d, $c = 'is')
	{
		$this->setWhere($this->tbl . '.title', $d, $c);
        return $this;
	}

    /*
	#设置商家ID
	*/
	public function setMerchantId($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'merchant_id', $d, $c);
        return $this;
	}

    /*
	#设置城市ID
	*/
	public function setCityId($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'sys_citys_id', $d, $c);
        return $this;
	}
	
 	/*
	#设置操作者ID
	*/
	public function setUserId($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'sys_users_id', $d, $c);
        return $this;
	}
	
	/*
	#设置商品以哪种方式成团
	*/
	public function setTeamMode($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'teammode', $d, $c);
        return $this;
	}
	
	/*
	#设置快递方案ID
	*/
	public function setExpressId($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'express_id', $d, $c);
        return $this;
	}
	
	/*
	#设置发货方式
	*/
	public function setDelivery($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'delivery', $d, $c);
        return $this;
	}
	
	/*
	#设置快递方案ID
	*/
	public function setExpressId($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'express_id', $d, $c);
        return $this;
	}
	
	/*
    #设置商品的状态
    */
    public function setState($d, $c='is')
    {
        return $this->setWhere($this->tbl . 'state', $d, $c);
        return $this;
    }

    /*
	#设置是否允许使用优惠券
	*/
	public function setIsCredit($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'iscredit', $d, $c);
        return $this;
	}
	
	/*
	#设置是否允许使用积分抵价
	*/
	public function setIsPoint($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'ispoint', $d, $c);
        return $this;
	}
	
	/*
	#设置是否前台隐藏
	*/
	public function setIsHide($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'ishide', $d, $c);
        return $this;
	}

    /*
	#设置商品总数
	*/
	public function setMaxNumber($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'maxnumber', $d, $c);
        return $this;
	}
	
	/*
	#设置商品最低成团数
	*/
	public function setMinNumber($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'minnumber', $d, $c);
        return $this;
	}
	
	/*
	#设置商品当前被购买的商品总数
	*/
	public function setNowGnumber($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'nowgnumber', $d, $c);
        return $this;
	}
	
	/*
	#设置商品当前被购买的买家总数（人）
	*/
	public function setNowPnumber($d, $c = 'is')
	{
		$this->setWhere($this->tbl . 'nowpnumber', $d, $c);
        return $this;
	}

	/*
	#设置商品被添加(创建)的时间
	*/
	public function setCreateTime($f, $t=0)
	{
		if(!empty($f) && !empty($t) && $f == $t){
			return $this->setWhere($this->tbl . 'createtime', $f);
		}else{
			if(!empty($f)){
	            $this->setWhere($this->tbl . 'createtime', $f, 'lmax');
	        }
	        if(!empty($t)){
	            $this->setWhere($this->tbl . 'createtime', $t, 'lmin');
	        }
		}
		return $this;
	}
	
	/*
	#设置商品开始时间
	*/
	public function setBeginTime($f, $t=0)
	{
		if(!empty($f) && !empty($t) && $f == $t){
			return $this->setWhere($this->tbl . 'begintime', $f);
		}else{
			if(!empty($f)){
	            $this->setWhere($this->tbl . 'begintime', $f, 'lmax');
	        }
	        if(!empty($t)){
	            $this->setWhere($this->tbl . 'begintime', $t, 'lmin');
	        }
		}
		return $this;
	}
	
	/*
	#设置商品因卖光而自然结束时间
	*/
	public function setEndtime($f, $t=0)
	{
		if(!empty($f) && !empty($t) && $f == $t){
			return $this->setWhere($this->tbl . 'endtime', $f);
		}else{
			if(!empty($f)){
	            $this->setWhere($this->tbl . 'endtime', $f, 'lmax');
	        }
	        if(!empty($t)){
	            $this->setWhere($this->tbl . 'endtime', $t, 'lmin');
	        }
		}
		return $this;
	}
	
	/*
	#设置商品关闭时间
	*/
	public function setColsetime($f, $t=0)
	{
		if(!empty($f) && !empty($t) && $f == $t){
			return $this->setWhere($this->tbl . 'closetime', $f);
		}else{
			if(!empty($f)){
	            $this->setWhere($this->tbl . 'closetime', $f, 'lmax');
	        }
	        if(!empty($t)){
	            $this->setWhere($this->tbl . 'closetime', $t, 'lmin');
	        }
		}
		return $this;
	}
	
	/*
	#设置商品过期时间
	*/
	public function setExpiretime($f, $t=0)
	{
		if(!empty($f) && !empty($t) && $f == $t){
			return $this->setWhere($this->tbl . 'expiretime', $f);
		}else{
			if(!empty($f)){
	            $this->setWhere($this->tbl . 'expiretime', $f, 'lmax');
	        }
	        if(!empty($t)){
	            $this->setWhere($this->tbl . 'expiretime', $t, 'lmin');
	        }
		}
		return $this;
	}
	
   




}



